package com.example.springredistest.controller;

import com.example.springredistest.tools.GetUserMain;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpDeviceService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("test")
@Slf4j
public class TestController {

    @Autowired
    GetUserMain getUserMain;
    /**
     * 这个是微信平台需要验证你服务器 调用你接口用的
     * // TODO: 2021/11/17  暂时没什么用 
     * @author tmz
     * @date 2021/11/17
     * @param [signature, timestamp, nonce] 
     * @return java.lang.Boolean
     */
    @GetMapping("check")
    public Boolean check(@RequestParam("signature")String signature,
                      @RequestParam("timestamp")String timestamp,
                      @RequestParam("nonce")String nonce
            ){
        WxMpService wxService = getUserMain.getWxService();
        WxMpUserService userService = wxService.getUserService();
        WxMpDeviceService deviceService = wxService.getDeviceService();
//        deviceService.authorize()
        return  wxService.checkSignature(timestamp, nonce, signature);
    }
    /**
     * 生成授权页面链接
     * @author tmz
     * @date 2021/11/17
     * @return java.lang.String
     */
    @GetMapping("url")
    public String  url(){
        WxMpService wxMpService = getUserMain.getWxService();
        //授权回调 链接 这个是你本地链接 {@link callback()}这个方法
        String url = "http://testtmz.free.idcfengye.com/test/callback";
        String res = wxMpService.getOAuth2Service().buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
        log.info("url: {}",res);
        return res;
    }
    /**
     * 上述回调 链接 调用的方法
     * @author tmz
     * @date 2021/11/17
     * @param request 
     * @param res 
     * @param code 这个是微信平台根据回调url传过来 加密个人信息 
     * @return void
     */
    @GetMapping("callback")
    public void callback(HttpServletRequest request, HttpServletResponse res , @RequestParam("code")String code) throws WxErrorException, IOException {
        //解密拿到 个人信息  里面有openid
        WxMpService wxMpService = getUserMain.getWxService();
        WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
        WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
        log.info("aaxx:{}",wxMpOAuth2AccessToken);
        log.info("userinfo:{}",wxMpUser);
        // TODO: 2021/11/17  拿到openid  你就带着openid 跳转到你的表单页面链接 你可以 http://xxxx:port/form?openid=xxxxx 传过去
//        res.sendRedirect("http://www.baidu.com"); 这个是跳转链接  （重定向）
    }

}
